/**********************************************************
 * File description:PreferencesService.cpp
 * Function:偏好设置服务类
 * Current Version: 1.0.0
 * Author: 夏新宇
 * Date: 2022-10-02
 *  -----
 *  Copyright by bokun Software.
 **********************************************************/
#include "PreferencesService.h"

PreferencesService::PreferencesService()
{
    conv = new Convertion();
    QByteArray tempByte = (QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME).toLocal8Bit();
    filePathLength = (QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME).length();
    filePath = tempByte.data();
    simplexml = new Simplexml(filePath);
    simplexml->next("preferences");
}

QHash<QString,QString> PreferencesService::getPreferencesData(){
    QHash<QString,QString> res;
    QString filePath = "";

    const char *xmlPath = simplexml->child("file-path")->text(); // 配置文件中文件路径

    LOG << "QString::fromUtf8(xmlPath)" << QString::fromUtf8(xmlPath);
    if(QString::fromUtf8(xmlPath) == ""){ // 如果配置文件写的文件路径为空
        filePath = QCoreApplication::applicationDirPath(); // 写入当前项目运行路径
    }else {
        filePath = QString::fromUtf8(xmlPath);
    }
    res["file-path"] = filePath;
    res["auto-open"] = simplexml->child("auto-open")->text();
    res["auto-export"] = simplexml->child("auto-export")->text();
    res["export-file-format"] = simplexml->child("export-file-format")->text();
    res["export-sample-info"] = simplexml->child("export-sample-info")->text();
    res["export-procedure-data"] = simplexml->child("export-procedure-data")->text();
    res["export-result"] = simplexml->child("export-result")->text();
    res["export-dissolution-result"] = simplexml->child("export-dissolution-result")->text();
    res["export-row-data"] = simplexml->child("export-row-data")->text();
    res["export-standard-data"] = simplexml->child("export-standard-data")->text();
    res["export-dissolution-data"] = simplexml->child("export-dissolution-data")->text();
    res["export-reagent-info"] = simplexml->child("export-reagent-info")->text();

    LOG << "res Perfences" << res;

    return res;
}

void PreferencesService::updatePreferences(QHash<QString,QString> updatedPreferencesData){
    LOG << "更新偏好设置数据" << endl;
    simplexml->child("file-path")->text(updatedPreferencesData["file-path"].toStdString().c_str());
    simplexml->child("auto-open")->text(updatedPreferencesData["auto-open"].toStdString().c_str());
    simplexml->child("auto-export")->text(updatedPreferencesData["auto-export"].toStdString().c_str());
    simplexml->child("export-file-format")->text(updatedPreferencesData["export-file-format"].toStdString().c_str());;
    simplexml->child("export-sample-info")->text(updatedPreferencesData["export-sample-info"].toStdString().c_str());
    simplexml->child("export-procedure-data")->text(updatedPreferencesData["export-procedure-data"].toStdString().c_str());
    simplexml->child("export-result")->text(updatedPreferencesData["export-result"].toStdString().c_str());
    simplexml->child("export-dissolution-result")->text(updatedPreferencesData["export-dissolution-result"].toStdString().c_str());
    simplexml->child("export-row-data")->text(updatedPreferencesData["export-row-data"].toStdString().c_str());
    simplexml->child("export-standard-data")->text(updatedPreferencesData["export-standard-data"].toStdString().c_str());
    simplexml->child("export-dissolution-data")->text(updatedPreferencesData["export-dissolution-data"].toStdString().c_str());
    simplexml->child("export-reagent-info")->text(updatedPreferencesData["export-reagent-info"].toStdString().c_str());

    try{
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME);
    }catch (string e){
        qDebug() << QString::fromStdString(e) << endl;
    }
}



